package main.java.br.com.projeto.domain.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import main.java.br.com.projeto.domain.entity.Servico;

public class DaoServico extends Dao<Servico> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5415567741342364158L;

	public DaoServico() {
		super(Servico.class);
	}
	
	public List<Servico> listByFilter(Long id, String filter) {
		String hql = "select servico from Servico servico "
				+ "where (lower(servico.descricao) like '%' || lower ( cast(:filter as string) ) || '%' or :filter = null) "
				+ "and (servico.id = :id or :id = 0)";

		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("filter", filter);
		parameters.put("id", id);

		return this.findOneResult(hql, parameters);
	}
	
	public List<Servico> listServicosFuncionarios (Long funcionarioId) {
		String hql = "select servico from Servico servico "
				+ "join servico.funcionarios funcionarios "
				+ "where funcionarios.id = :funcionarioId";

		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("funcionarioId", funcionarioId);
		
		return this.findOneResult(hql, parameters);
	}
	
	public List<Servico> listServicosFuncionariosByQuery( Long funcionarioId, String query )
	{
		String hql = "select servico from Servico servico "
				+ "join servico.funcionarios funcionarios "
				+ "where funcionarios.id = :funcionarioId "
				+ "and (lower(servico.descricao) like '%' || lower ( cast(:query as string) ) || '%')";

		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("funcionarioId", funcionarioId);
		parameters.put("query", query);
		
		return this.findOneResult(hql, parameters);
	}
	
	public List<Servico> listServicosByQuery( String query )
	{
		String hql = "select servico from Servico servico "
				+ "where (lower(servico.descricao) like '%' || lower ( cast(:query as string) ) || '%')";

		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("query", query);
		
		return this.findOneResult(hql, parameters);
	}
	
	public List<Servico> findByFilterAndFuncionario( Long id, String filter, Long funcionarioId )
	{
		String hql = "select servico from Servico servico "
				+ "join servico.funcionarios funcionarios "
				+ "where (lower(servico.descricao) like '%' || lower ( cast(:filter as string) ) || '%' or :filter = null) "
				+ "and (servico.id = :id or :id = 0) "
				+ "and (funcionarios.id = :funcionarioId)";

		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("filter", filter);
		parameters.put("id", id);
		parameters.put("funcionarioId", funcionarioId);

		return this.findOneResult(hql, parameters);
	}
}
